package cn.org.timespace.ons.action.base;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import cn.org.timespace.easyframe.action.EFBaseAction;
import cn.org.timespace.easyframe.util.generator.IdentityGenerator;

import cn.org.timespace.ons.service.base.AppleServerService;
import cn.org.timespace.ons.service.base.MachinesApplyService;
import cn.org.timespace.ons.service.base.MachinesService;
import cn.org.timespace.ons.service.base.OptionsService;
import cn.org.timespace.ons.util.pager.Pager;
import cn.org.timespace.ons.util.pager.PagerHelper;
import cn.org.timespace.ons.vo.base.AppleServer;
import cn.org.timespace.ons.vo.base.Options;


import com.opensymphony.xwork2.ActionContext;
/*
 * 应用平台管理
 * author:ync
 */
public class AppleServerAction extends EFBaseAction{

	private static final long serialVersionUID = -3416183313291298070L;
	private static Log log=LogFactory.getLog(AppleServerAction.class);
	private AppleServerService appleServerService;
	private OptionsService tsOptionsService;
	private MachinesService machinesService;
	private MachinesApplyService machinesApplyService;
	
	
	
	
	public MachinesApplyService getMachinesApplyService() {
		return machinesApplyService;
	}
	public void setMachinesApplyService(MachinesApplyService machinesApplyService) {
		this.machinesApplyService = machinesApplyService;
	}
	public MachinesService getMachinesService() {
		return machinesService;
	}
	public void setMachinesService(MachinesService machinesService) {
		this.machinesService = machinesService;
	}
	public OptionsService getTsOptionsService() {
		return tsOptionsService;
	}
	public void setTsOptionsService(OptionsService tsOptionsService) {
		this.tsOptionsService = tsOptionsService;
	}
	public AppleServerService getAppleServerService() {
		return appleServerService;
	}
	public void setAppleServerService(AppleServerService appleServerService) {
		this.appleServerService = appleServerService;
	}
	public AppleServerAction()
	{
		super(new AppleServerModel());
	}
	AppleServerModel appleServerModel=(AppleServerModel)this.getModel();
	
	
	public String checkName() throws IOException{
		String result = "true"; //存在
		boolean flag=appleServerService.isExistByTypeName(appleServerModel.getName(),getUserAccount());//同一用户下类型名称不可以相同
		if(flag){
			result="true";
		}else{
			result="false";
		}
		getResponse().setCharacterEncoding("UTF-8");
		PrintWriter out = getResponse().getWriter();
		out.write(result);
		return null;
	}
	
	/**
	 * 显示应用列表
	 */
	public String doList() throws Exception{
		String theIdUserApply = getRequest().getParameter("theIdUserApply");
		appleServerModel.setTheIdUserApply(theIdUserApply);
		appleServerModel.setUserName(getUserAccount());
		if(appleServerModel.getStatus()==null){
			appleServerModel.setStatus(1);
		}
		Pager p = PagerHelper.getPagerInstance(getRequest());
		System.out.println("名称为："+appleServerModel.getName());
		List<Object> resList = appleServerService.findByCondition(appleServerModel,p);
		if(resList != null && resList.size() == 2){
			p.setTotalCount(((Integer)resList.get(0)));
			p.setData((List)resList.get(1));
		}
	
		
		
		return LIST;
	}
	
	public String doFindByCompCode() throws Exception{
		String theIdUserApply = getRequest().getParameter("theIdUserApply");
		appleServerModel.setTheIdUserApply(theIdUserApply);
		appleServerModel.setUserName(getUserAccount());
		Pager p = PagerHelper.getPagerInstance(getRequest());
		List<Object> resList = appleServerService.findByCondition(appleServerModel,p);
		if(resList != null && resList.size() == 2){
			p.setTotalCount(((Integer)resList.get(0)));
			p.setData((List)resList.get(1));
		}
	
		
		return LIST;
	}
	

	public String doAddList() throws Exception{
		Pager p = PagerHelper.getPagerInstance(getRequest());
		
		List<Object> resList = appleServerService.findByCondition(appleServerModel,p);
		if(resList != null && resList.size() == 2){
			p.setTotalCount(((Integer)resList.get(0)));
			p.setData((List)resList.get(1));
		}
	
		return "addseleteapp";
	}
	
	
	/**
	 * 更新应用
	 * @return
	 * @throws Exception
	 */
	public String doUpdate() throws Exception {
		AppleServer appleServer = appleServerModel.getAppServer();
		//type暂时不使用，随便保存一个默认值
		appleServer.setType("000");
		appleServer.setUserName(getUserAccount());
		String sheng =	appleServer.getSelA();//省
	    String shi =	appleServer.getSelB();//市
	    String qu = appleServer.getSelC();//县/区
	    String id = "";
	    if(qu!=null&&qu.length()>0){
	    	 id = qu;
	    }else if(shi!=null&&shi.length()>0){
	    	
	    	 id = shi;	
	    }else if(sheng!=null&&sheng.length()>0){
	    	 id = sheng;
	    }
	    if(id!=null&&id.length()>0){
	    	if(!appleServer.getDqM().equals(id)){
	    		appleServer.setDqM(id);
	    	}
	    	
	    }
		appleServerService.update(appleServer);
		return doList();
	}
	
	/**
	 * 增加应用
	 * @return
	 * @throws Exception
	 */
	public String doSave() throws Exception{
		AppleServer appleServer = appleServerModel.getAppServer();
		appleServer.setUserName(getUserAccount());
		
		//type暂时不使用，随便保存一个默认值
		appleServer.setType("000");
		//主键生成策略
		
		String asid =IdentityGenerator.getInstance().getappId();
		appleServer.setAsid(asid);//主键手动生成
		//增加地区
		String sheng =	appleServer.getSelA();//省
	    String shi =	appleServer.getSelB();//市
	    String qu = appleServer.getSelC();//县/区
	    String id = "";
	    if(qu!=null&&qu.length()>0){
	    	 id = qu;
	    }else if(shi!=null&&shi.length()>0){
	    	
	    	 id = shi;	
	    }else if(sheng!=null&&sheng.length()>0){
	    	 id = sheng;
	    }
	    if(id!=null&&id.length()>0){
	    	appleServer.setDqM(id);
	    }
		appleServerService.save(appleServer);
		
		appleServer.setAsid("");      //应用Id
		
		appleServer.setInterType("0");
		appleServer.setInterAddress("");
		appleServer.setInterMethod("");
		
		return ADD;
	}
	
	/**
	 * 删除应用,软删除，status=0表示下线
	 * @return
	 * @throws Exception
	 */
	public String doDelete() throws Exception{

		String asId = getRequest().getParameter("asId") ;//应用Id，用于查询后删除
		AppleServer appleServer = appleServerService.findById(asId);
		appleServer.setStatus(0);//0下线1上线
		appleServerService.update(appleServer);
		
		return doList();
	}
	
	
	public String doFindById() throws Exception{
		
		String asId = getRequest().getParameter("asId") ;//应用Id
		OptionsModel opt = new OptionsModel();
		opt.setCode(getRequest().getParameter("code"));
		List<Options> roleControlPointList = tsOptionsService.findByCondition(opt);
		getRequest().setAttribute("optionsList", roleControlPointList);
		if(!StringUtils.isEmpty(asId)){  //更新
			AppleServer appleServer = appleServerService.findById(asId);
			appleServerModel.setAppServer(appleServer);
			
			
			return UPDATE;
		}
		
		return ADD;
		
	}
	
	public String doListApple() throws Exception{
		Pager p = PagerHelper.getPagerInstance(getRequest());
		List<Object> resList = appleServerService.findByCondition(appleServerModel,p);
		if(resList != null && resList.size() == 2){
			p.setTotalCount(((Integer)resList.get(0)));
			p.setData((List)resList.get(1));
		}
	
		return LIST;
	}
	
	/**
	 * 校验用户账号是否存在
	 * @return 存在返回 true 不存在返回false
	 * @throws Exception
	 */
	public String doCheckUserAccountIsExist() throws Exception{
		String result = "true"; //存在
		String compCode = getRequest().getParameter("compCode");
		AppleServer AppleServer = appleServerService.findByCompCode(compCode);
		if(AppleServer == null){
			result = "false"; //不存在
		}
		
		getResponse().setCharacterEncoding("UTF-8");
		PrintWriter out = getResponse().getWriter();
		out.write(result);
		
		return null;
	}
	/**
	 * 根据machines查询设备类型
	 */
	 public List<Options> findByCode(){
		
		List<Options> list = this.appleServerService.findByCode("apple");
		return list;
	}
	
	
	 /*
	  * 显示是否上线
	  */
	 
	 public void doCheckPortAndIPIsExist() throws Exception{
		 String result = "true";//存在
		 String asid = getRequest().getParameter("asid");
		 String IP = getRequest().getParameter("IP");
		 String port = getRequest().getParameter("port");
		 
		 AppleServer appleServer = this.appleServerService.findByCondition(IP, port);
		 if(appleServer ==null || (asid!=null && asid.equals(appleServer.getAsid()))){
			 result = "false";//不存在
		 }
		 
		 	getResponse().setCharacterEncoding("UTF-8");
			PrintWriter out = getResponse().getWriter();
			out.write(result);
			
	 }
	 
	 /**
 	 * 下线前先判断还有没关联
 	 * @throws IOException
 	 */
 	public void checkIsOffLine() throws IOException{
 		String asId = getRequest().getParameter("asId");
 		String flag = "0";//0可以下线
 			flag = machinesApplyService.findByAsid(asId);//0可以下线，大于0不可以下线
 			 getResponse().setCharacterEncoding("UTF-8");
 	   			PrintWriter out = getResponse().getWriter();
 	   			out.write(flag);
 	}
	 

}
